﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Interop;
using Microsoft.Win32;
using WebFlipScreenSaver.Model;
using WebFlipScreenSaver.View;

namespace WebFlipScreenSaver
{
	public partial class App : System.Windows.Application 
	{
		private HwndSource _winWPFContent;
		private ScreenSaverView _winSaver;
		private static bool _isPreviewMode;

		static System.Threading.Mutex _mtx ;
		/// <summary>
		/// InitializeComponent
		/// </summary>
		public void InitializeComponent()
		{
			//May Need this to debug the screen saver
			//Thread.Sleep(30000);


			if (Environment.GetCommandLineArgs().Length == 2 && Environment.GetCommandLineArgs()[1].ToLower().StartsWith("/c"))
			{
				bool created;
				_mtx = new System.Threading.Mutex(true, "WebFlipScreenSaver", out created);

				// If we created the mutex then admin mode isn't already runing.  Otherwise
				// no StartupUri and we will die.
				if (created)
					StartupUri = new Uri("/View/SettingsView.xaml", System.UriKind.Relative);
			}
			else if (Environment.GetCommandLineArgs().Length == 2 && Environment.GetCommandLineArgs()[1].ToLower().StartsWith("/s"))
				StartupUri = new Uri("/View/ScreenSaverView.xaml", UriKind.Relative);
			else if (Environment.GetCommandLineArgs().Length == 3 && Environment.GetCommandLineArgs()[1].ToLower().StartsWith("/p"))
			{
				_isPreviewMode = true;

	
				_winSaver = new ScreenSaverView();
				var previewHandle = Convert.ToInt32(Environment.GetCommandLineArgs()[2]);

                var pPreviewHnd = new IntPtr(previewHandle);

                var lpRect = new RECT();

                bool bGetRect = Win32API.GetClientRect(pPreviewHnd, ref lpRect);
				var sourceParams = new HwndSourceParameters("sourceParams")
				                   	{
				                   		PositionX = 0,
				                   		PositionY = 0,
				                   		Height = lpRect.Bottom - lpRect.Top,
				                   		Width = lpRect.Right - lpRect.Left,
				                   		ParentWindow = pPreviewHnd,
				                   		WindowStyle =
				                   			(int)
				                   			(WindowStyles.WS_VISIBLE | WindowStyles.WS_CHILD | WindowStyles.WS_CLIPCHILDREN)
				                   	};



				_winWPFContent = new HwndSource(sourceParams);
                _winWPFContent.Disposed += new EventHandler(winWPFContent_Disposed);

                _winWPFContent.RootVisual = _winSaver.RootGrid;

			}



			Current.DispatcherUnhandledException += Current_DispatcherUnhandledException;
		}

        void winWPFContent_Disposed(object sender, EventArgs e)
        {
            
			_winSaver.Close();
            Application.Current.Shutdown();
        }

		/// <summary>
		/// Application Entry Point.
		/// </summary>
		[System.STAThreadAttribute()]
		public static void Main()
		{
			App app = new App();
			app.InitializeComponent();

			if (app.StartupUri != null || _isPreviewMode)
				app.Run();
			else if(!_isPreviewMode) app.Shutdown(0);

			if (_mtx != null) _mtx.Close();
		}

		private void Current_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
		{
			MessageBox.Show("Fatal error: " + e.Exception.Message + "\n" + e.Exception.StackTrace);
		}
	}
}
